
package net.phoenix.repository.nodetype;

import java.io.File;
import java.io.IOException;

import net.phoenix.repository.data.NodeDefinitionData;
import net.phoenix.repository.data.NodeTypeData;
import net.phoenix.repository.data.PropertyDefinitionData;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;
/**
 * 
 * @author lixf
 *
 */
public class XMLNodeTypeParser {

	public XMLNodeTypeParser() {
		
	}

	public NodeTypeData parser(File file) throws IOException, SAXException {
		Digester digester = new Digester();
		digester.setValidating(false);
		digester.setNamespaceAware(false);
		NodeTypeData definition = new NodeTypeData();
		digester.push(definition);
	
		digester.addSetProperties("type", "name", "name");
		digester.addSetProperties("type", "supertypes", "superTypeList");
		digester.addSetProperties("type", "abstract", "isAbstract");
		digester.addSetProperties("type", "mixin", "mixin");
		digester.addSetProperties("type", "hasOrderableChildNodes", "hasOrderableChildNodes");
		digester.addSetProperties("type", "queryable", "queryable");
		digester.addSetProperties("type", "primaryItemName", "primaryItemName");
		digester.addBeanPropertySetter("type/descrpition", "description");
		//property definition parser
		digester.addObjectCreate("type/property", PropertyDefinitionData.class);
		digester.addSetProperties("type/property", "name", "name");

		digester.addSetProperties("type/property", "type", "typeName");
		digester.addSetProperties("type/property", "autoCreated", "autoCreated");
		digester.addSetProperties("type/property", "mandatory", "mandatory");
		digester.addSetProperties("type/property", "protected", "isProtected");
		
		digester.addSetProperties("type/property", "multiple", "multiple");
		digester.addSetProperties("type/property", "minLength", "minLength");
		digester.addSetProperties("type/property", "maxLength", "maxLength");
		digester.addSetProperties("type/property", "orderNo", "orderNo");
		digester.addSetProperties("type/property", "readOnly", "readOnly");
		digester.addSetProperties("type/property", "required", "required");
		digester.addSetProperties("type/property", "searchable", "searchable");
		digester.addSetProperties("type/property", "orderable", "orderable");
		
		digester.addCallMethod("type/property/default-value", "addDefaultValue", 1);
		digester.addCallParam("type/property/default-value", 0);
		digester.addSetNext("type/property", "addPropertyDefinition");
		
		//child node definition parser
		digester.addObjectCreate("type/child", NodeDefinitionData.class);
		digester.addSetProperties("type/child", "name", "name");
		digester.addSetProperties("type/child", "type", "nodeType");
		digester.addSetProperties("type/child", "autoCreated", "autoCreated");
		digester.addSetProperties("type/child", "mandatory", "mandatory");
		digester.addSetProperties("type/child", "protected", "isProtected");
		digester.addSetProperties("type/child", "allowsSameNameSiblings", "allowsSameNameSiblings");

		digester.addSetNext("type/child", "addNodeDefinition");
		
		digester.parse(file);
		return definition;
	}


}
